package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.ResultInfo;
import com.itheima.reggie.domain.Dish;
import com.itheima.reggie.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @author zsk
 * @create 2022-03-04-16:51
 */
@RestController
public class DishController {

    //依赖注入
    @Autowired
    private DishService dishService;

    /*
    显示数据（查询现有菜品）
    Request URL: http://localhost:8080/dish/page?page=1&pageSize=10
    Request Method: GET
     */
    @GetMapping("/dish/page")
    public ResultInfo findByPage(@RequestParam(value = "page",defaultValue = "1") Integer pageNum,//当前页
                                 @RequestParam(defaultValue = "10") Integer pageSize,//每页条数
                                 String name){//可能根据姓名查询
        //调用service查询
        Page<Dish> dishPage = dishService.findByPage(pageNum,pageSize,name);
        //返回结果
        return ResultInfo.success(dishPage);
    }

    /*
    新增菜品数据
    Request URL: http://localhost:8080/dish
    Request Method: POST
     */
    @PostMapping("/dish")
    public ResultInfo save(@RequestBody Dish dish){

        //调用service层添加
        dishService.save(dish);
        //返回结果
        return ResultInfo.success(null);
    }

    /*
    回显菜品信息
    Request URL: http://localhost:8080/dish/1499739584492232705
    Request Method: GET
     */
    @GetMapping("/dish/{id}")
    public ResultInfo findById(@PathVariable("id") Long id){
        //调用service查询
        Dish dish = dishService.findById(id);

        //返回结果
        return ResultInfo.success(dish);
    }

    /*
    修改菜品信息
    Request URL: http://localhost:8080/dish
    Request Method: PUT
     */
    @PutMapping("/dish")
    public ResultInfo update(@RequestBody Dish dish){
        //调用service层修改
        dishService.update(dish);

        //返回结果
        return ResultInfo.success(null);
    }

    /*
    修改菜品状态
    Request URL: http://localhost:8080/dish/status/0?ids=1499739584492232705
    Request Method: POST
     */
    @PostMapping("/dish/status/{status}")
    public ResultInfo updateStatus(@PathVariable("status") Integer status,
                                   Long[] ids){
        //调用service层修改
        dishService.updateStatus(status,ids);

        //返回结果
        return ResultInfo.success(null);

    }

    /*
    删除菜品信息
    Request URL: http://localhost:8080/dish?ids=1499739584492232705
    Request Method: DELETE
     */
    @DeleteMapping("/dish")
    public ResultInfo deleteById(Long[] ids){
        //调用service层删除
        dishService.deleteById(ids);

        //返回结果
        return ResultInfo.success(null);

    }

}
